About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Round
Round a numeric value to the nearest integer.
Installation
npm install @stdlib/math-base-special-round
Usage
var round = require( '@stdlib/math-base-special-round' );
round( x )
Rounds a numeric
value to the nearest integer
.
var v = round( -4.2 );
v = round( -4.5 );
v = round( -4.6 );
v = round( 9.99999 );
v = round( 9.5 );
v = round( 9.2 );
v = round( 0.0 );
v = round( -0.0 );
v = round( Infinity );
v = round( -Infinity );
v = round( NaN );
Notes
- Ties are rounded toward positive infinity.
Examples
var randu = require( '@stdlib/random-base-randu' );
var round = require( '@stdlib/math-base-special-round' );
var x;
var i;
for ( i = 0; i < 100; i++ ) {
x = (randu()*100.0) - 50.0;
console.log( 'Value: %d. Rounded: %d.', x, round( x ) );
}
C APIs
Usage
#include "stdlib/math/base/special/round.h"
stdlib_base_round( x )
Rounds a numeric
value to the nearest integer
.
double out = stdlib_base_round( -4.2 );
The function accepts the following arguments:
- x:
[in] double
input value.
double stdlib_base_round( const double x );
Examples
#include "stdlib/math/base/special/round.h"
#include <stdio.h>
int main( void ) {
const double x[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
double v;
int i;
for ( i = 0; i < 10; i++ ) {
v = stdlib_base_round( x[ i ] );
printf( "round(%lf) = %lf\n", x[ i ], v );
}
}
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.
0.3.0 (2024-07-27)
<section class="features">
Features
0227bf0
- add C implementation for math/base/special/round
</section>
<!-- /.features -->
<section class="bug-fixes">
Bug Fixes
947e77f
- handle large numbers correctly and revert to Math.round for JS
</section>
<!-- /.bug-fixes -->
<section class="commits">
Commits
<details>
62e703a
- docs: add missing @private
in math/base/special/round/lib/native.js
(#2536) (by Gunj Joshi)133dd8b
- chore: update package meta data (#2251) (by stdlib-bot)c37109b
- test: fix missing argument (by Athan Reines)947e77f
- fix: handle large numbers correctly and revert to Math.round for JS (by Philipp Burckhardt)0227bf0
- feat: add C implementation for math/base/special/round
(by Pranav Goswami, Philipp Burckhardt)
</details>
</section>
<!-- /.commits -->
<section class="contributors">
Contributors
A total of 4 people contributed to this release. Thank you to the following contributors:
- Athan Reines
- Gunj Joshi
- Philipp Burckhardt
- Pranav Goswami
</section>
<!-- /.contributors -->
</section>
<!-- /.release -->
<section class="release" id="v0.2.1">